package com.blog.wl2023.repository;

import com.blog.wl2023.entity.Article;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wule
 * @desc
 */
@Repository
public interface ArticleRepository extends JpaRepository<Article, Long>,JpaSpecificationExecutor<Article> {



	@Query("SELECT a FROM Article a WHERE a.uuid = :uuid")
	Article findByUUid(String uuid);

	@Query("SELECT new Article(a.uuid,a.content,a.author,a.updateTime) FROM  Article a WHERE a.uuid = '001'")
	Article findByUUidInAbout();
	@Modifying
	@Transactional
	@Query("update Article a set a.content=:content where a.uuid = :uuid")
	void updateAboutCount(String uuid,String content);

	@Modifying
	@Transactional
	@Query("update Article a set a.views=a.views + 1 where a.uuid = :uuid")
	void updateViews(String uuid);

	@Modifying
	@Transactional
	@Query("update Article a set a.commentSize=a.commentSize + 1 where a.uuid = :uuid")
	void updateArticleCommentSizeByUUid(String uuid);

	@Query("select new Article(a.title,a.summary) from Article a where a.uuid<>'001' and a.isDelete = 0")
	List<Article> getAllTitles();

	@Modifying
	@Transactional
	@Query("update Article a set a.isTop=:isTop where a.uuid = :uuid")
	void updateArticleIsTop(String uuid, Integer isTop);
	@Modifying
	@Transactional
	@Query("update Article a set a.isDelete=:isDelete where a.uuid = :uuid")
	void updateArticleIsDelete(String uuid, Integer isDelete);

	@Modifying
	@Transactional
	@Query("update Article a set a.title=:title,a.summary=:summary,a.content=:content,a.typeId=:typeId,a.author=:author,a.quoteLink=:quoteLink,a.imageUrl=:imageUrl,a.keyOne=:keyOne  where a.uuid = :uuid")
	void updateArticle(String uuid,
	                   String title,
	                   String summary,
	                   String content,
	                   String typeId,
	                   String author,
	                   String quoteLink,
	                   String imageUrl,
	                   String keyOne);
}
